//
//  ZCPayshareViewController.m
//  zctx
//
//  Created by apple on 15/10/26.
//  Copyright © 2015年 sanliang. All rights reserved.
//

#import "ZCPayshareViewController.h"
#import "ZCPayshareOneCell.h"
#import "ZCPayshareTwoCell.h"
#import "ZCInvolvedDetailsVC.h"
@interface ZCPayshareViewController ()<UITableViewDataSource,UITableViewDelegate,shareButtonDelegate>
@property (nonatomic,strong)UITableView *tableView;
@end

@implementation ZCPayshareViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"ZCPayshareViewController"];
        [MTA trackPageViewBegin:@"ZCPayshareViewController"];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"ZCPayshareViewController"];
    [MTA trackPageViewEnd:@"ZCPayshareViewController"];
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"sharepay" object:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.navigationItem setTitle:@"支付成功"];
    
    [self initnavegation];
    [self initTableView];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(sharepaysuccess) name:@"sharepay" object:nil];
}

#pragma mark 导航栏
-(void)initnavegation{
    
    UIButton * leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 19, 36);
    [leftButton setImage:[UIImage imageNamed:@"navigation_back"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * barButtonItem_leftNavBar = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = barButtonItem_leftNavBar;
}


-(void)backButtonPressed{
    
    [self.navigationController popToRootViewControllerAnimated:YES];
}

-(void)sharepaysuccess{
    if ([self.markStr isEqualToString:@"1"]) {
        theAppdelegate.tbc.selectedIndex = 0;
        [self.navigationController popToRootViewControllerAnimated:YES];
    }else{
        
        for (UIViewController *temp in self.navigationController.viewControllers) {
            if ([temp isKindOfClass:[ZCInvolvedDetailsVC class]]) {
                [self.navigationController popToViewController:temp animated:YES];
            }
        }
    }
    
}


-(void)initTableView{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.sectionFooterHeight = 0.0001;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(0, 0,0,0));
    }];
    [self.tableView registerNib:[UINib nibWithNibName:@"ZCPayshareOneCell" bundle:nil] forCellReuseIdentifier:@"onecell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"ZCPayshareTwoCell" bundle:nil] forCellReuseIdentifier:@"twocell"];
}

-(void)initFootView{
    
    UIView * footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 100)];
    UIButton *okButton = [UIButton new];
    [footView addSubview:okButton];
    okButton.layer.cornerRadius = 5.0f;
    [okButton setTitle:@"返回首页" forState:UIControlStateNormal];
    okButton.backgroundColor = [UIColor colorWithHexString:@"#03Ab9e"];
    [okButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(footView.top).with.offset(40);
        make.left.equalTo(footView).with.offset(50);
        make.right.equalTo(footView).with.offset(-50);
        make.height.mas_equalTo(@40);
    }];
    okButton.userInteractionEnabled = YES;
    [okButton addTarget:self action:@selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    self.tableView.tableFooterView = footView;
}

#pragma mark UITableViewDataSource,UITableViewDelegate

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = nil;
    if (indexPath.section==0) {
        static NSString *onecellid = @"onecell";
        ZCPayshareOneCell *cell  = [tableView dequeueReusableCellWithIdentifier:onecellid];
        if (cell==nil) {
           cell = [[ZCPayshareOneCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:onecellid];
        }
        return cell;
    }else{
        static NSString *twocellid = @"twocell";
        ZCPayshareTwoCell *cell = [tableView dequeueReusableCellWithIdentifier:twocellid];
        if (cell==nil) {
            cell = [[ZCPayshareTwoCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:twocellid];
        }
        cell.delegate = self;
        return cell;
    }
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        return 120;
    }else{
        return 170;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section==0) {
        return 0.0001;
    }else{
        return 10;
    }
}


-(void)wechatButtonClick:(NSInteger)tag{
    NSLog(@"sdll = %ld",(long)tag);
    
    NSString *strType = nil;
    switch (tag) {
        case 0:
            strType = @"0";
            break;
        case 1:
            strType = @"1";
            break;
        case 2:
            strType = @"2";
            break;
        case 3:
            strType = @"2";
            break;
        case 4:
            strType = @"3";
            break;
        default:
            break;
    }
    
    [NetworkManager requestFoundingGetShareWithMid:UserModel.mid password:UserModel.password fdid:self.pid type:strType success:^(NSDictionary *dictResponse) {
        
        NSLog(@"dict = %@",dictResponse);
        WXMediaMessage *message = [WXMediaMessage message];
        message.title = [NSString stringWithFormat:@"%@",[dictResponse objectForKey:@"title"]];
        NSLog(@"mesage.title = %@",message.title);
        message.description = [NSString stringWithFormat:@"%@",[dictResponse objectForKey:@"desc"]];
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[dictResponse objectForKey:@"imgUrl"]]]];
        message.thumbData = data;
        
        WXWebpageObject *ext = [WXWebpageObject object];
        ext.webpageUrl = [NSString stringWithFormat:@"%@",[dictResponse objectForKey:@"link"]];;
        
        message.mediaObject = ext;
        SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
        req.bText = NO;
        req.message = message;
        if (tag == 1) {
            req.scene = WXSceneTimeline;
            [WXApi sendReq:req];
        } else if (tag == 0) {
            req.scene = WXSceneSession;
            [WXApi sendReq:req];
        }
        
    } failure:^(NSError *error) {
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
